using UnityEngine;

public class ChaseState : IState
{
    // 构造函数
    public ChaseState(FSM manager)
    {
        this.manager = manager;
        this.parameter = manager.parameter;
    }

    public override  void OnEnter()
    {
        //TODO:如果有奔跑动画，当然切换为奔跑动画最好
        parameter.animator.Play("Walk");
    }

    public override void OnUpdate()
    {
        // 如果被击中了，转换到受击状态
        if (parameter.getHit)
        {
            manager.TransitionState(StateType.Hit);
        }

        // 如果目标不存在或者超出追逐范围，则转换到空闲状态
        if (parameter.target == null ||
            manager.transform.position.x < parameter.chasePoints[0].position.x ||
            manager.transform.position.x > parameter.chasePoints[1].position.x)
        {
            manager.TransitionState(StateType.Idle);
        }

        // 如果检测到攻击范围内有目标，则转换到攻击状态
        if (Physics2D.OverlapCircle(parameter.attackPoint.position, parameter.attackArea, parameter.targetLayer))
        {
            manager.TransitionState(StateType.Attack);
        }
    }

    
    public override void OnFixedUpdate()
    {
        manager.FlipTo(parameter.target);    // 面向目标
        // 向目标位置移动
        if (parameter.target != null)
        {
            manager.transform.position = Vector2.MoveTowards(manager.transform.position,
                parameter.target.position, parameter.chaseSpeed * Time.deltaTime);
        }
    }

    public override void OnExit()
    {
        
    }

}
